from pao_python.pao.service.security.security_utility import PermissionUtility
from app.permission import Permission
from service.user_func import User_Server
from service.classify import Classify


def register(jsonrpc, db_addr, db_name, db_port, session, inital_password):
    classify_management = Classify(db_addr, db_port, db_name)
    Permission_utility = PermissionUtility(db_addr, db_port, db_name, session)
    User_func = User_Server(db_addr, db_port, db_name,
                            inital_password, session)
    PERMISSIOM = '有此权限'

    # 增加/修改
    @jsonrpc.method('IProductClassifyService.insert')
    def insert_service(data):
        def pass_permission():
            is_insert = False
            # 新建
            if 'id' not in data.keys():
                user_list = User_func.get_current_user_by_account_id(
                    session['account_id'])
                data['user_id'] = user_list['id']
                is_insert = True
            res = classify_management.insert(data, is_insert)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)

    # 获取父级分类列表信息
    @jsonrpc.method('IProductClassifyService.get_parent_list')
    def get_parent_list_service(condition, page, size):
        '''产品分类列表查询'''
        def pass_permission():
            res = classify_management.get_parent_list(
                condition, page, size)
            return res
        if Permission_utility.is_permission(Permission.AllSign_Select) == PERMISSIOM:
            return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)
        else:
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            condition['user_id'] = user_list['id']
            return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)

    # 获取分类列表信息
    @jsonrpc.method('IProductClassifyService.get_product_classify_list')
    def get_product_classify_list_service(condition, page, size):
        '''产品分类列表查询'''
        def pass_permission():
            res = classify_management.get_product_classify_list(
                condition, page, size)
            return res
        if Permission_utility.is_permission(Permission.AllSign_Select) == PERMISSIOM:
            return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)
        else:
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            condition['user_id'] = user_list['id']
            return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)

    # 删除
    @jsonrpc.method('IProductClassifyService.delete')
    def delete_service(handle_id):
        def pass_permission():
            res = classify_management.delete(handle_id)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)

    # 根据ID获取产品分类信息
    @jsonrpc.method('IProductClassifyService.get_product_classify')
    def get_product_classify_service(id):
        '''获取企业信息'''
        def pass_permission():
            res = classify_management.get_product_classify(id)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)

    # 查询分类及其父级的所有自定义类目列表信息
    @jsonrpc.method('IProductClassifyService.get_custom_attribute_list')
    def get_custom_attribute_list(id):
        def pass_permission():
            res = classify_management.get_custom_attribute_list(id)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ProductClassify_Select)
